package thread.call;

/**
 * @author yuisama
 * @date 2022/07/13 11:08
 **/
public class RunnableCount {
    private static class Count {
        int sum = 0;
        Object lock = new Object();
    }

    public static void main(String[] args) throws InterruptedException {
        Count count = new Count();
        Thread t = new Thread(() -> {
            int sum = 0;
            for (int i = 0; i <= 1000; i++) {
                sum += i;
            }
            synchronized (count.lock) {
                count.sum = sum;
                // 唤醒主线程
                count.lock.notify();
            }
        });
        t.start();
        // 主线程阻塞，等待子线程执行完毕之后，获取计算结果
        synchronized (count.lock) {
            if (count.sum == 0) {
                count.lock.wait();
            }
        }
        System.out.println("子线程执行结束，结果为 : " + count.sum);
    }
}